package org.newcih.mail;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

public class Main {

    public static String myEmailAccount = "13267711536@163.com";
    public static String myEmailPassword = "newcih2017";
    public static String myEmailSMTPHost = "smtp.163.com";
    public static String receiveMailAccount = "newcih@qq.com";


   /*
    public static void main(String[] args) throws IOException, MessagingException {
        Properties properties = new Properties();
        Session session = Session.getDefaultInstance(properties);
        MimeMessage mimeMessage = new MimeMessage(session);

        mimeMessage.setFrom(new InternetAddress("newcih@qq.com", "USER_AA", "UTF-8"));

        mimeMessage.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress[]{new InternetAddress("1573390117@qq.com", "USER_CC", "UTF-8")});

        mimeMessage.setSubject("TEST 邮件主题", "UTF-8");

        mimeMessage.setContent("TEST 邮件正文。。。", "text/html;charset=UTF-8");

        mimeMessage.setSentDate(new Date());

        mimeMessage.saveChanges();

        OutputStream out = new FileOutputStream("MyEmail.eml");
        mimeMessage.writeTo(out);
        out.close();
    }
    */

    public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.host", myEmailSMTPHost);
        properties.setProperty("mail.smtp.auth", "true");


        for (int i = 0; i < 23; i++) {
            Session session = Session.getDefaultInstance(properties);
            session.setDebug(true);

            MimeMessage mimeMessage = createMimeMessage(session, myEmailAccount, receiveMailAccount);

            Transport transport = session.getTransport();

            transport.connect(myEmailAccount, myEmailPassword);

            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

            transport.close();
        }
    }

    public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws UnsupportedEncodingException, MessagingException {
        MimeMessage mimeMessage = new MimeMessage(session);

        mimeMessage.setFrom(new InternetAddress(sendMail, new Random().nextDouble()+"某报网", "UTF-8"));

        mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX用户", "UTF-8"));

        mimeMessage.setSubject(new Random().nextDouble() + "", "UTF-8");

        mimeMessage.setContent(new Random().nextDouble() + "XX用户您好，今天全场5折，快来抢购，错过今天再等一年。。。", "text/html;charset=utf-8");

        mimeMessage.setSentDate(new Date());

        mimeMessage.saveChanges();

        return mimeMessage;
    }
}
